package view;

import view.conponent.*;
import viewModel.CurrentUser;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class MainInterface extends JFrame {

    public MainInterface(String title) throws HeadlessException {
        super(title);

        this.pack();
        JPanel view = new JPanel();
        JPanel top = new JPanel();
        JTree tree = new JTree();
        JLabel label = new JLabel();
        top.add(label);
        JSplitPane splitPane = new JSplitPane();
        JLabel currentTitle = new JLabel();
        top.add(Box.createHorizontalStrut(30));
        top.add(currentTitle);
        currentTitle.setFont(new Font("果方",Font.PLAIN,20));
        top.setLayout(new FlowLayout(FlowLayout.LEFT));
        top.setMinimumSize(new Dimension(1080,100));
        top.setMaximumSize(new Dimension(1080,100));

        view.setLayout(new BorderLayout());
        view.add(top,BorderLayout.NORTH);
        splitPane.setLeftComponent(tree);
        view.add(splitPane);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(view);
        final int WIDTH = 1200;
        final int HEIGHT = 800;
        this.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - WIDTH) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - HEIGHT) / 2,
                WIDTH, HEIGHT);
        this.setResizable(false);
        try {
            this.setIconImage(ImageIO.read(new File("images/Transparent_mini_LOGO.png")));
            label.setIcon(new ImageIcon(ImageIO.read(new File("images/Transparent_LOGO.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }

        splitPane.setContinuousLayout(true);
        splitPane.setMinimumSize(new Dimension(1080,900));
        splitPane.setDividerLocation(200);
        splitPane.setDividerSize(8);
        JPanel jp1 = new JPanel();
        jp1.setBounds(0,0,500,500);
        splitPane.setRightComponent(jp1);

        tree.setMinimumSize(new Dimension(200, 600));
        tree.removeAll();
        tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("东软颐养社区中心系统"){
            {
                DefaultMutableTreeNode category;
                DefaultMutableTreeNode book;

                category = new DefaultMutableTreeNode("用户管理");
                add(category);
                {
                    book = new DefaultMutableTreeNode("病患管理");
                    category.add(book);
                    book = new DefaultMutableTreeNode("床位管理");
                    category.add(book);
                    book = new DefaultMutableTreeNode("稀有设备管理");
                    category.add(book);
                }
                category = new DefaultMutableTreeNode("评估管理");
                add(category);
                {
                    book = new DefaultMutableTreeNode("问题管理");
                    category.add(book);
                    book = new DefaultMutableTreeNode("模板管理");
                    category.add(book);
                }
                category = new DefaultMutableTreeNode("楼宇管理");
                add(category);
                {
                    book = new DefaultMutableTreeNode("房间管理");
                    category.add(book);
                }
                category = new DefaultMutableTreeNode("职工管理");
                add(category);
                {
                    book = new DefaultMutableTreeNode("医生管理");
                    category.add(book);
                    book = new DefaultMutableTreeNode("护士管理");
                    category.add(book);
                    book = new DefaultMutableTreeNode("护工管理");
                    category.add(book);
                }
            }
        }));

        tree.addTreeSelectionListener(e -> {
            //当条目选中后执行
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
            if(selectedNode!=null){
                String str = selectedNode.toString();
                currentTitle.setText(str);
                if ("admin".equals(CurrentUser.user.getUserName())) {
                    switch (str) {
                        case "病患管理", "床位管理", "稀有设备管理", "问题管理", "模板管理" -> {
                            JOptionPane.showMessageDialog(splitPane, "请登陆职工账号");
                            splitPane.setRightComponent(null);
                        }
                        case "房间管理" -> splitPane.setRightComponent(new RoomManager());
                        case "医生管理" -> splitPane.setRightComponent(new StaffManager(0));
                        case "护士管理" -> splitPane.setRightComponent(new StaffManager(1));
                        case "护工管理" -> splitPane.setRightComponent(new StaffManager(2));
                    }
                } else {
                    switch (str) {
                        case "病患管理" -> splitPane.setRightComponent(new PatientManager());
                        case "床位管理" -> splitPane.setRightComponent(new BedManager());
                        case "稀有设备管理" -> splitPane.setRightComponent(new EquipmentManager());
                        case "问题管理" -> splitPane.setRightComponent(new QuestionManager());
                        case "模板管理" -> splitPane.setRightComponent(new TemplateManager());
                        case "房间管理", "医生管理", "护士管理", "护工管理" -> {
                            JOptionPane.showMessageDialog(splitPane, "权限不足");
                            splitPane.setRightComponent(null);
                        }
                        default -> splitPane.setRightComponent(null);
                    }
                }
            }
        });

        JMenuBar jmb = new JMenuBar();
        JMenu jm = new JMenu("账户");
        JMenuItem m1 = new JMenuItem("切换账号");
        JMenuItem m2 = new JMenuItem("退出程序");
        jm.add(m1);
        jm.add(m2);
        jmb.add(jm);
        m1.addActionListener(e -> {
            CurrentUser.user = null;
            new LoginInterface();
            MainInterface.this.dispose();
        });
        m2.addActionListener(e -> {
            CurrentUser.user = null;
            System.exit(0);
        });

        this.setJMenuBar(jmb);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        new MainInterface("test");
    }
}

